//
//  HTCopilotCell.m
//  TruckDriver
//
//  Created by Ting Han on 2017/11/24.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCopilotCell.h"
#import "UIImageView+WebCache.h"
@interface HTCopilotCell()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *flagLabel;
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;

@end
@implementation HTCopilotCell

- (void)setUnbindModel:(HTUnbindCopilotModel *)unbindModel{
    _unbindModel = unbindModel;
    self.nameLabel.text = unbindModel.name;
    self.phoneLabel.text = unbindModel.phone;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:unbindModel.picurl] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
    //0主驾邀请1副驾邀请
    if ([unbindModel.from isEqualToString:@"0"]) {
        self.flagLabel.text = @"我的邀请";
        self.flagLabel.backgroundColor = kGreenColor;
        [self.firstButton setTitle:@"撤销邀请" forState:UIControlStateNormal];
        [self.secondButton setTitle:@"联系他" forState:UIControlStateNormal];
    }else{
        self.flagLabel.text = @"司机申请";
        self.flagLabel.backgroundColor = kBlueColor;
        [self.firstButton setTitle:@"拒绝申请" forState:UIControlStateNormal];
        [self.secondButton setTitle:@"接受申请" forState:UIControlStateNormal];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 25;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark --- 网络请求

- (IBAction)firstButtonAction:(id)sender {
    if ([self.unbindModel.from isEqualToString:@"0"]){//主驾 撤销邀请
        [UIAlertController alertStyleShowInController:self.controller  title:@"温馨提示" message:[NSString stringWithFormat:@"确认撤销邀请的副驾：\n%@吗？",self.phoneLabel.text] alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
            [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self netLoadReject];
            }]];
        } completion:^{
            
        }];
     
    }else{//副驾 拒绝申请
        [UIAlertController alertStyleShowInController:self.controller  title:@"温馨提示" message:[NSString stringWithFormat:@"确认拒绝副驾：\n%@【%@】的申请吗？",self.nameLabel.text,self.phoneLabel.text] alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
            [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self netLoadReturn];
            }]];
        } completion:^{
            
        }];
    }
}
- (IBAction)secondButtonAction:(id)sender {
    if ([self.unbindModel.from isEqualToString:@"0"]){//主驾邀请 接收申请
        [UIAlertController alertStyleShowInController:self.controller  title:@"温馨提示" message:[NSString stringWithFormat:@"确认同意副驾：\n%@【%@】的申请吗？",self.nameLabel.text,self.phoneLabel.text] alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
            [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self netLoadApply];
            }]];
        } completion:^{
            
        }];
        
    }else{//副驾申请 联系他
        
        NSString *phone = self.unbindModel.phone;
        NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
    
}
- (void)netLoadApply{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestCopilotApplyWithDriverid:self.unbindModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"主驾同意申请：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"同意申请失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.detialBlock) {
            self.detialBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"同意申请失败"];
    }];
}
- (void)netLoadReturn{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestCopilotReturnApplyWithDriverid:self.unbindModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"主驾撤销申请：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"撤销申请失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.detialBlock) {
            self.detialBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"撤销申请失败"];
    }];
}
//主驾拒绝
- (void)netLoadReject{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestCopilotRejectApplyWithType:@"0" driverid:self.unbindModel.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"主驾拒绝申请：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"拒绝申请失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
         [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.detialBlock) {
            self.detialBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"拒绝申请失败"];
    }];
  
}



@end
